
<template>

	<view class="index">
		<u-top-tips ref="uTips"></u-top-tips>
		<view class="boxLogin">
			<view >
				<text class="h2">登录</text>
			</view>
			<view  class="mt-10">
				<small>在这里，发现大世界(*^▽^*)</small>
			</view>
			<view >
				<u-form :model="form" class="formLogin mt-20" :rules="rules" ref="uForm">
					<u-form-item prop="phone">
						<u-input v-model="form.phone" placeholder="手机号" />
					</u-form-item>
					<u-form-item prop="password">
						<u-input v-model="form.password" type="password" placeholder="密码(新用户默认密码123456)" />
					</u-form-item>
					<u-button class="mt-20" type="primary" @click="toLogin">登录</u-button>
				</u-form>
			</view>
			<view >
				<view class="jb login-footer mt-20">
					<navigator url="../Regsiter/Regsiter">注册账户</navigator>
					<navigator url="">忘记密码</navigator>
				</view>
			</view>
		</view>
	</view>


</template>

<script>
	export default {
		name: 'Login',
		data() {
			return {
				form: {
					phone: '',
					password: '',
					longitude:'',
					latitude:'',
				},
				rules: {
					phone: [{
							required: true,
							message: '请输入手机号',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						},
						{
							pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
							message: '请输入正确的手机格式',
							trigger: ['change', 'blur'],
						}
					],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'change'
					}]
				}
			};
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		created() {
			this.getLoaction()
		},
		methods: {
			getLoaction(){//uniapp内置获得当前地理位置的方法
				uni.getLocation({
				    type: 'wgs84',
					geocode:true,//设置该参数为true可直接获取经纬度及城市信息
				    success:(res)=> {
						this.form.latitude=res.latitude
						this.form.longitude=res.longitude
				    },
					fail :() =>{
						uni.showToast({
						    title: '获取地址失败，将导致部分功能不可用',
							icon:'none'
						});
					}
				});
	
			},
			toLogin() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						//登录组件
						this.$api.login(this.form).then(
							(res) => {	
								if(res!==0){
									uni.setStorageSync('token',res.token)
									uni.setStorageSync('phone',this.form.phone)
									uni.setStorageSync('password',this.form.password)
									this.$refs.uTips.show({
										duration: 3000,
										title: '登录成功',
										type: 'success'
									});
									uni.navigateTo({
										url: '../index/index',
									});
								}
									
							},
							err=>{
								console.log(err)
							}
						);

					} else {
						console.log('验证失败');
					}
				})

			}
		}
	};
</script>

<style lang="scss">
	
	.boxLogin {
		margin: 100rpx auto;
		width: 80vw;
		.formLogin {
			width: 80vw;
		}

		.login-footer {
			width: 80vw;
		}
      .h2{
		  font-size: 50rpx;
	  }
	}
</style>
